import math
from Blender import Mathutils
	
class Soldier:

	def __call__(self):
		pass
	
	def __init__(self,cont):
		self.cont=cont
		self.obj=cont.owner
		self.speed=0.00155
		
	def activateAction(self,name,end,type=None):
		action=self.cont.getActuator(name)
		action.setStart(1)
		action.setEnd(end)
		self.obj['last_frame']=end
		if type!=None:
			action.setType(type)
		self.cont.activate(action)

	def deactivateAction(self,name):
		self.cont.deactivate(self.cont.getActuator(name))
	
	def activateMotion(self,name,DLoc=None,DRot=None):
		motion=self.cont.getActuator(name)
		if(DLoc!=None):
			a,b,c,d=DLoc
			motion.setDLoc(a,b,c,d)
		if(DRot!=None):
			a,b,c,d=DRot
			motion.setDRot(a,b,c,d)
		if DLoc != None or DRot != None:
			self.cont.activate(motion)	

	def deactivateMotion(self,name):
		motion=self.cont.getActuator(name)
		self.cont.deactivate(motion)
	
	def doAction(self,checkFrame,changeState,name,end,type=None):
		if not checkFrame or self.obj['frame']==self.obj['last_frame']:
			self.activateAction(name,end,type)
			if changeState:
				self.obj['state']+=1		
				
	def doMotion(self,checkFrame,name,DLoc=None,DRot=None):
		if not checkFrame or self.obj['frame']==self.obj['last_frame']:
			self.activateMotion(name,DLoc,DRot)

	def sit_down(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'sit_down',15)
		print self.obj['frame'],self.obj['last_frame']
			
	def do_something(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'do_something',50)
			
	def stand_up(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'stand_up',15)

	def start_walk(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'start_walk',10)	
		self.obj['walk_loops']=0

	def walk(self,checkFrame=True,changeState=True,type=None):
		self.doAction(checkFrame,changeState,'walk',26)
		
	def stop_walk(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'stop_walk',10)	
	
	def rotate(self,dx,dy,dz,checkFrame=True):
		self.doMotion(checkFrame,'turn',DRot=(dx,dy,dz,0))
			
	def move(self,dx,dy,dz,checkFrame=True,loc=1):
		self.doMotion(checkFrame,'turn',DLoc=(dx,dy,dz,loc))
		
	def init_state(self,checkFrame=True,changeState=True):
		self.doAction(checkFrame,changeState,'init_state',2)
		
	def turn_right(self,checkFrame=True,checkState=True):
		self.doAction(checkFrame,checkState,'turn_right',24)
		
	def travel_forward(self,startLoc,endLoc):
		if self.obj['dx']==0 and self.obj['dy']==0 and self.obj['dz']==0:
			self.obj.localPosition=startLoc
			self.obj['dx'],self.obj['dy'],self.obj['dz']=[s-e for s,e in zip(startLoc,endLoc)]
			l=math.sqrt(self.obj['dx']**2+self.obj['dy']**2+self.obj['dz']**2)
			self.obj['dx']/=l;
			self.obj['dy']/=l;
			self.obj['dz']/=l;
			angleX=-math.degrees(math.asin(self.obj['dz']))
			if(self.obj['dx']<0):
				if(self.obj['dy']<0):
					angleZ=180-math.degrees(math.asin(self.obj['dx']))
				else:
					angleZ=math.degrees(math.asin(self.obj['dx']))
			else:
				if(self.obj['dy']<0):
					angleZ=180-math.degrees(math.asin(self.obj['dx']))
				else:
					angleZ=math.degrees(math.asin(self.obj['dx']))
			euler=Mathutils.Euler(angleX,0,angleZ)
			matrix=euler.toMatrix()
			print matrix

			self.obj.localOrientation=matrix
			
			self.obj['dx']*=-self.speed;
			self.obj['dy']*=-self.speed;
			self.obj['dz']*=-self.speed;
			
			self.obj['wantedX'],self.obj['wantedY'],self.obj['wantedZ']=endLoc
		self.walk(changeState=False,type=4)
			
		self.move(self.obj['dx'],self.obj['dy'],self.obj['dz'],checkFrame=False,loc=0)
		self.checkMotion()
		
	def checkMotion(self):
		pos=self.obj.localPosition
				
		dist=(self.obj['wantedX']-pos[0])**2+(self.obj['wantedY']-pos[1])**2+(self.obj['wantedZ']-pos[2])**2
		if dist<self.speed**2:
			pos[0]=self.obj['wantedX']
			pos[1]=self.obj['wantedY']
			pos[2]=self.obj['wantedZ']
			self.obj.localPosition=pos
			self.deactivateAction('walk')

			self.obj['dx']=0
			self.obj['dy']=0
			self.obj['dz']=0
			self.move(self.obj['dx'],self.obj['dy'],self.obj['dz'],checkFrame=False,loc=0)
			self.obj['state']+=1
				

from Soldier import Soldier
from Blender import Mathutils
import math

class Script9_lotok_op1(Soldier):
	def __init__(self,cont):
		Soldier.__init__(self,cont)		

	def __call__(self):		
		if self.obj['state']==0:
			self.init_state();			
		elif self.obj['state']==1:
			self.turn_right();
			self.rotate(0,0,-0.025,checkFrame=False)
		elif self.obj['state']==2:
			self.rotate(0,0,0)
			self.move(0,-self.speed,0)
			self.start_walk()
		elif self.obj['state']==3:			
			if self.obj['frame']==self.obj['last_frame']:
				if self.obj['walk_loops']==3:
					self.obj['state']+=1
				else:
					self.obj['walk_loops']+=1
			
			if self.obj['walk_loops']==1:
				if self.obj['frame']>=15:
					self.rotate(0,0,0.035,checkFrame=False)						
			elif self.obj['walk_loops']==3:
				pass
			if self.obj['walk_loops']<=3:
				self.walk(changeState=False)		

		elif self.obj['state']==4:
			self.rotate(0,0,0,checkFrame=False)
			self.stop_walk()

		elif self.obj['state']==5:
			if self.obj['frame']==self.obj['last_frame']:				
				GameLogic.getCurrentScene().objects['OBoperator2']['state']=0
			self.move(0,0,0)
			self.rotate(0,0,0.03)
			self.sit_down()
			
		elif self.obj['state']==6:
			self.rotate(0,0,0)
			self.do_something()
		
		elif self.obj['state']==7:
			self.rotate(0,0,-0.027)
			self.stand_up()
		
		elif self.obj['state']==8:
			if self.obj['frame']==self.obj['last_frame']:
				euler = Mathutils.Euler(0,0,0)
				matrix = euler.toMatrix()
				self.obj.localOrientation=matrix
			self.rotate(0,0,0)
			"""
			self.move(0,-self._speed,0)
			self.start_walk()
			"""
			self.init_state()
		
		elif self.obj['state']==9:
			self.travel_forward(self.obj.localPosition,[-0.683,0.466,0.198])

		
		elif self.obj['state']==10:
			self.stop_walk()
			
		elif self.obj['state']==11:
			self.move(0,0,0)
			self.rotate(0,0,0.03)
			self.sit_down()
			
		elif self.obj['state']==12:
			self.rotate(0,0,0)
			self.do_something()
			
		elif self.obj['state']==13:
			self.rotate(0,0,-0.03)
			self.stand_up()
		else:
			self.rotate(0,0,0)
	
		
			
cont=GameLogic.getCurrentController()
script=Script9_lotok_op1(cont)
script()
del script

from Soldier import Soldier
import math
from Blender import Mathutils

class Script9_lotok_op2(Soldier):
	def __init__(self,cont):
		Soldier.__init__(self,cont)		

	def __call__(self):		
		if self.obj['state']==0:
			self.init_state();			
			
		elif self.obj['state']==1:
			self.move(0,0,0)
			self.sit_down()
			
		elif self.obj['state']==2:
			self.rotate(0,0,0)
			self.do_something()
		
		elif self.obj['state']==3:
			self.rotate(0,0,-0.035)
			self.stand_up()
		
		elif self.obj['state']==4:
			if self.obj['frame']==self.obj['last_frame']:
				euler = Mathutils.Euler(0,0,180)
				matrix = euler.toMatrix()
				self.obj.localOrientation=matrix
			self.rotate(0,0,0)
			self.rotate(0,0,0)
			"""
			self.move(0,-self._speed,0)
			self.start_walk()
			"""
			self.init_state()		
		
		elif self.obj['state']==5:
			self.travel_forward(self.obj.localPosition,[-0.6,1.068,0.198])
		
		elif self.obj['state']==6:
			self.stop_walk()
			
		elif self.obj['state']==7:
			self.move(0,0,0)
			self.rotate(0,0,0.03)
			self.sit_down()
			
		elif self.obj['state']==8:
			self.rotate(0,0,0)
			self.do_something()
			
		elif self.obj['state']==9:
			self.rotate(0,0,-0.03)
			self.stand_up()
		else:
			self.rotate(0,0,0)
			
		
			
cont=GameLogic.getCurrentController()
script=Script9_lotok_op2(cont)
script()
del script

